//
//  WDWorkOrderSearchViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/19.
//

#import "WDWorkOrderSearchViewController.h"
#import "WDWorkOrderSearchViewModel.h"
#import "WDWorkOrderSearchTopView.h"
#import "WDWorkOrderSearchMarketView.h"
#import "WDWorkOrderItemViewCell.h"
#import "WDWorkOrderItemFirstViewCell.h"

@interface WDWorkOrderSearchViewController ()

@property (nonatomic, strong) WDWorkOrderSearchViewModel *viewModel;

@property (nonatomic, strong) WDWorkOrderSearchTopView *topView;

/// 搜索记录
@property (nonatomic, strong) WDWorkOrderSearchMarketView *marketView;

@end

@implementation WDWorkOrderSearchViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDWorkOrderSearchViewModel new];
    _viewModel.searchMsg = self.searchMsg;
    
    [_viewModel setupData];
    
    _viewModel.reqModel.searchType = self.searchType;
    
    [self.viewModel updateWithSearchKeyWord:self.searchMsg];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
    self.baseTableView.height = self.baseTableView.height - kWDWorkOrderSearchTopViewHeight;
    self.baseTableView.top = kWDWorkOrderSearchTopViewHeight;
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.baseTableView.backgroundColor = [UIColor whiteColor];
    
    [self.baseTableView registerClass:[WDWorkOrderItemViewCell class] forCellReuseIdentifier:[WDWorkOrderItemViewCell className]];
    [self.baseTableView registerClass:[WDWorkOrderItemFirstViewCell class] forCellReuseIdentifier:[WDWorkOrderItemFirstViewCell className]];
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self hiddenFooterView:YES];
    
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - kWDWorkOrderSearchTopViewHeight;
    self.rootContent.top = kWDWorkOrderSearchTopViewHeight;
    
    [self initWithServiceTopView];
    
    [self initWithMarketView];
}

- (void)initWithServiceTopView
{
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDWorkOrderSearchTopViewHeight);
        _topView = [[WDWorkOrderSearchTopView alloc] initWithFrame:frame];
        [self.view addSubview:_topView];
        @weakify(self);
        _topView.touchSearchBlock = ^(NSString * _Nonnull searchMsg) {
            @strongify(self);
            [self actionWithSearchMsg: searchMsg];
        };
    }
//    _topView.searchMsg = self.viewModel.searchMsg;
}

- (void)initWithMarketView {
    @weakify(self);
    if (!_marketView) {
        CGRect frame = CGRectMake(0, 11, kScreenW, kWDWorkOrderSearchMarketViewMinHeight);
        _marketView = [[WDWorkOrderSearchMarketView alloc] initWithFrame:frame title:@"历史搜索"];
        _marketView.hiddenClearBtn = NO;
        [self.rootContent addSubview:_marketView];
        
        _marketView.touchMarketBlock = ^(NSString * _Nonnull typeTitle, NSString * _Nonnull title, NSInteger index) {
            @strongify(self);
            [self touchMarket:typeTitle title:title index:index];
        };
        
        _marketView.touchClearMarketBlock = ^{
            @strongify(self);
            [self actionClearHistoryRecord];
        };
    }
    
    [_marketView updateWithViewData:self.viewModel.markets completion:^(float height) {
        @strongify(self);
        [self updateSubViewPosition];
    }];
}

/// 搜索
/// - Parameter msg: msg description
- (void)actionWithSearchMsg:(NSString *)msg {
    if ([objectNull(msg) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入搜索内容！"];
        return;
    }
    [self.viewModel updateWithSearchKeyWord:msg];
    self.viewModel.reqModel.content = msg;
    
    [self doRefreshData];
}

/// 清空历史记录
- (void)actionClearHistoryRecord
{
    [self.viewModel clearHistoryRecord];
    [self initWithMarketView];
}

/// 点击标签
/// @param typeTitle 类型标题
/// @param title 标题
/// @param index index
- (void)touchMarket:(NSString *)typeTitle title:(NSString *)title index:(NSInteger)index
{
//    self.rootContent.hidden = YES;
    [self.viewModel updateWithSearchKeyWord:title];
    self.topView.searchMsg = title;
    self.viewModel.searchMsg = title;
    [self actionWithSearchMsg:title];
}

/// 更新子视图位置
- (void)updateSubViewPosition
{
//    self.hotMarketView.top = self.historyMarketView.bottom + 11;
    
//    self.rootContent.contentSize = CGSizeMake(kScreenW, self.hotMarketView.bottom + 10 + IPHONEX_BOTTOM_SPACE);
}

- (void)doRefreshData
{
    [super doRefreshData];
    self.viewModel.reqModel.current = 1;
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    
    @weakify(self);
    [self.viewModel requestWithTaskSearch:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self.baseTableView reloadData];
        }
        [self endAllRefreshing];
        
        if (self.viewModel.dataList.count < 10) {
            [self hiddenFooterView:YES];
        } else {
            [self hiddenFooterView:NO];
        }
        
        if (self.viewModel.dataList.count > 0) {
            self.rootContent.hidden = YES;
        } else {
            self.rootContent.hidden = NO;
        }
        
    }];
     
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return kWDWorkOrderItemFirstViewCellHeight;
    }
    return kWDWorkOrderItemFirstViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        WDWorkOrderItemFirstViewCell *cell = [WDWorkOrderItemFirstViewCell cellForTableView:tableView indexPath:indexPath];
        [cell updateCellData:model];
        return cell;
    }
    WDWorkOrderItemViewCell *cell = [WDWorkOrderItemViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10030101) parentCode:@(100301)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//        [self doRefreshData];
    };
    
    NSDictionary *params = @{
        @"serviceModel": model,
        @"block":block
    };
    
//    [SVProgressHUD showInfoWithStatus:@"详情页面暂未开发！"];
    UIViewController *vc = nil;
    // 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
    if (model.taskType == 1) { // 外出救援
        if (model.servFlow == 0) { // 待分派
            vc = [[TYZRouter sharedInstance] getGoutWaitAssignVC:params];
        } else if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getGrabWaitVehicleVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getGrabReviewVehicleVC: params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getGrabMaintenanceVehicleVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGrabPendingVehicleVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        }
    } else { // 站内维修
        if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehicleVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getServiceOrderReviewVehicleVC: params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getServiceOrderMaintenanceVehicleVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehicleVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehDetailVC:params];
        }
    }
    /*
    if (model.taskType == 1) { // 外出救援
        if (model.servFlow == 0) { //待分派
            vc = [[TYZRouter sharedInstance] getGoutWaitAssignDetailVC:params];
        } else if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getGoutWaitVehicleDetailVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getGoutReviewVehicleDetailVC:params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getGoutMaintenanceVehicleDetailVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        }
    } else { // 站内维修
        if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehDetailVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getServiceOrderReviewVehDetailVC:params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getServiceOrderMainVehDetailVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehDetailVC:params];
        }
    }*/
    
    if (vc) {
        [TYZJumpVCManager pushBaseVC:self pushVC:vc];
    }
}


@end
